/**
 * Created by syf on 2015/12/3.
 */
var net = require('net');
var http= require('http');
//var HOST = '127.0.0.1';
var PORT = 8089;

var path = require('path');
var  express= require('express');
var app =express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('port', 9000);

var connect = require('connect');
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.use(connect.session({ secret: 'jiami', cookie: { maxAge: 20*60*1000}}));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
var  io;
//var server= net.createServer(function (sock) {
//
//    io=sock;
//
//    var remoteAddress = sock.remoteAddress;
//    var remotePort = sock.remotePort;
//
//    console.log('连上了: ' +
//    remoteAddress + ':' + remotePort);
//
//    sock.on('data', function (data) {
//        console.log('客户端说：' + sock.remoteAddress + ': ' + data);
//        sock.write('服务端应答：' + data + '"');
//    });
//    //sock.on('close', function (data) {
//    //    console.log('关闭: ' +
//    //    remoteAddress + ' ' + remotePort);
//    //});
//
//}).listen(PORT, HOST);
var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(9000);

//  Sever --> Client 的单向通讯

// 在前者的基础上，实现 Client --> Sever 的通讯，如此一来便是双向通讯
//  Sever --> Client 的单向通讯

// 在前者的基础上，实现 Client --> Sever 的通讯，如此一来便是双向通讯
var chatServer = net.createServer(),
    clientList = [],
clientList_list=[];

chatServer.on('connection', function(client) {
    io=client;

    // JS 可以为对象自由添加属性。这里我们添加一个 name 的自定义属性，用于表示哪个客户端（客户端的地址+端口为依据）
    client.name = client.remoteAddress + ':' + client.remotePort;
    clientList.push(client.name);
    clientList_list.push(client);
    console.log("我记录了"+client.name);
    client.write('Hi ' + client.name + '!\n');
    clientList.push(client);
    client.on('data', function(data) {
        console.log('客户端：' + client.name + '说' + data);
        client.write('服务端应答：' + data + '"');
    });
    client.on('end', function() {
        clientList.splice(clientList.indexOf(client), 1); // 删除数组中的制定元素。这是 JS 基本功哦~
    });
    client.on('error', function() {
        clientList.splice(clientList.indexOf(client), 1); // 删除数组中的制定元素。这是 JS 基本功哦~
    });
});
//function broadcast(message, client) {
//    for(var i=0;i<clientList.length;i+=1) {
//        if(client !== clientList[i]) {
//            clientList[i].write(client.name + " says " + message);
//        }
//    }
//}

chatServer.listen(PORT);

//var server = http.createServer(app);
//server.listen(PORT);

console.log('服务器正在监听 本机:' + PORT);

app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
var routes = require('./router.js');
app.use('/', routes);


module.exports={
    returnIO:function(){
        return io;
    },
    returnCL:function(){
        return clientList;
    },
    returnCLient:function(){
        return clientList_list;
    }
}

//server.get({path : "/say" , version : '0.0.1'} , function(req,res,next){
//
//    sock.on('data', function (data) {
//        sock.write('服务端应答：fdgfdgfdgfd');
//    });
//});